package com.china08.yxyapi.api.manage.tec.teaching.scoreport.createexam;

import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.china08.yxyapi.api.YxyApiBaseController;
import com.china08.yxyapi.entity.db.res.YxyResVersion;
import com.china08.yxyapi.model.YxyApiReqModel;
import com.china08.yxyapi.model.YxyApiRespModel;
import com.china08.yxyapi.repository.db.YxyResBookRepository;
import com.china08.yxyapi.repository.db.YxyResVersionRepository;

/**
 * 查询当前学科对应的版本号
 * 
 * @author yyy
 *
 */
// @Api(tags={"v90","版本"})
@CrossOrigin
@RestController
public class CreateExamVersonController extends YxyApiBaseController {

    @Autowired
    private YxyResBookRepository yxyResBookRepository;

    @Autowired
    private YxyResVersionRepository yrvr;

    @Autowired
    private YxyResBookRepository yrbr;

    @RequestMapping(value = "/api_yw/manage/tec/teaching/scoreport/createexam/verson/srh", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public RespModel precess(HttpServletRequest request, HttpServletResponse response, @RequestBody ReqModel reqModel) {

        String phaseName = reqModel.subjectName.split("-")[0];
        String phaseId = "01";
        switch (phaseName) {
        case "幼儿园":
            phaseId = "00";
            break;
        case "小学":
            phaseId = "01";
            break;
        case "初中":
            phaseId = "02";
            break;
        case "高中":
            phaseId = "03";
            break;
        }
        String subjectId = reqModel.subjectName.split("-")[2];

        List<YxyResVersion> versions = new ArrayList<YxyResVersion>();
        List<VersonModel> list = new ArrayList<VersonModel>();
        List<String> versionIds = yrbr.findVersionIdByPhaseIdAndSubjectIdGroupByVersionId(phaseId, subjectId);
        if (versionIds.size() > 0) {
            versions = yrvr.findByIdInAndStatusOrderByOrderNum(versionIds, 1);
        }
        for (YxyResVersion yxyResType : versions) {
            VersonModel vModel = new VersonModel();
            vModel.versonId = yxyResType.getId();
            vModel.versonName = yxyResType.getName();
            list.add(vModel);
        }
        RespModel model = new RespModel();
        model.courseNameLst = list;
        return model;

    }

    protected static class RespModel extends YxyApiRespModel {
        // 当前课程对应的版本号
        public List<VersonModel> courseNameLst;
    }

    protected static class ReqModel extends YxyApiReqModel {
        // 科目名称
        public String subjectName;
    }

    protected class VersonModel {
        // 版本id
        public String versonId;
        // 版本name
        public String versonName;
    }
}
